// EBASSystem.cpp : Implementation of CEBASSystem
#include "stdafx.h"
#include "EBASdll.h"
#include "EBASSystem.h"
#include "BrugerSession.h"
#include "Sikkerhed.h"
#include "importado.h"
#include "FileLogging.h"
#include "ErrorHandling.h"

/////////////////////////////////////////////////////////////////////////////
// CEBASSystem

STDMETHODIMP CEBASSystem::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IBrugerSession
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CEBASSystem::LogPaa(BSTR bruger, BSTR kode, long adminOption, IBrugerSession ** ppVal)
{
	HRESULT hr;
	bool validLicens = FALSE, validTrial = FALSE;

	try {
		CSikkerhed * pSik = CSikkerhed::GetInstance();
/*
		validLicens = pSik->VerificerLicens(EBAS_STANDARD);
		validTrial = pSik->VerificerLicens(EBAS_TRIAL);

		if (FALSE == validLicens && FALSE == validTrial) {
			CFileLogging::WriteToFile("CEBASSystem::LogPaa - Ugyldig licens", TRUE);
			CErrorHandling::GetErrorHandling()->SetErrorMessage("Ugyldig licens");
			return E_ACCESSDENIED;
		}
*/
		bstr_t session = pSik->VerificerBruger(bruger, kode, adminOption);

		if (bstr_t("") == session) {
			CFileLogging::WriteToFile("CEBASSystem::LogPaa - Ugyldig adgangskode", TRUE);
	
			CErrorHandling::GetErrorHandling()->SetErrorMessage("Logon Mislykkes");
			return E_ACCESSDENIED;
		}
		
		gpSessionGuid = session;

		CComObject<CBrugerSession> * pSession;
		CComObject<CBrugerSession>::CreateInstance(&pSession);
		pSession->SetBrugerInfo(bruger, kode);
		pSession->SetSessionInfo(session);
		// todo - set navn	

		if (S_OK == (hr = pSession->QueryInterface(ppVal))) {
			return hr;
		}
	} catch (_com_error &e) {
		CErrorHandling::GetErrorHandling()->SetErrorMessage("Logon Mislykkes");

		long l = (long)e.Error();
		char temp[16];
		ltoa(l, temp, 10);

		CFileLogging::WriteToFile(temp);
		CFileLogging::WriteToFile(e.ErrorMessage());
		CFileLogging::WriteToFile(e.Description());

		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEBASSystem::HentLicensInfo(BSTR *licensTil, BSTR *licensNr, BSTR *licensType, BSTR *slutDato)
{
	bool res;
	bstr_t t = bstr_t(""), l = bstr_t(""), d = bstr_t(""), typ = bstr_t("");
	try {
		CSikkerhed * pSikker = CSikkerhed::GetInstance();
		res = pSikker->HentLicensInfo(&t, &l, &typ, &d);

		if (FALSE == res)
			return E_FAIL;

		*licensTil = t.copy();
		*licensNr = l.copy();
		*slutDato = d.copy();
		*licensType = typ.copy();

	} catch (_com_error &e) {
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEBASSystem::NyLicens(BSTR licensTil, BSTR licensNr, BSTR licensSlut, LONG type)
{
	bool res = FALSE;
	try {
		CSikkerhed * pSikker = CSikkerhed::GetInstance();
		res = pSikker->GemLicensInfo(licensTil, licensNr, licensSlut, type);

		if (FALSE == res) {
			return E_FAIL;
		}

	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEBASSystem::get_LastError(BSTR *pVal)
{
	try {
		IErrorInfoPtr pErr;
		GetLastError();
		GetErrorInfo(NULL, &pErr);

		pErr->GetDescription(pVal);
	} catch (...) {
		*pVal = bstr_t("").copy();
	}

	return S_OK;
}

STDMETHODIMP CEBASSystem::get_Version(BSTR *pVal)
{
	*pVal = gpVersion.copy();

	return S_OK;
}
